<?php
class Silk_Zgb_AjaxController extends Mage_Core_Controller_Front_Action{
    public function IndexAction() {
        echo "OK";
        $website = Mage::getModel("core/website")->load(8);
        var_dump($website->getData());
    }

    public function citysAction()
    {
        $id = $this->getRequest()->getParam('id');

        $citys = array();
        $collection = Mage::getModel('chinaarea/city')->getCollection()->addFieldToFilter('region_id',array('eq'=>$id));
        $citys[] = array('value'=>'','label'=>'-请选择-');
        foreach($collection as $city)
        {
            $citys[] = array('value'=>$city->getId(), 'label'=>$city->getDefaultName());
        }
        echo json_encode($citys);
    }

    public function areasAction()
    {
        $id = $this->getRequest()->getParam('id');
        $areas = array();
        $collection = Mage::getModel('chinaarea/area')->getCollection()->addFieldToFilter('city_id',array('eq'=>$id));
        $areas[] = array('value'=>'','label'=>'-请选择-');
        foreach($collection as $area)
        {
            $areas[] = array('value'=>$area->getId(), 'label'=>$area->getDefaultName());
        }
        echo json_encode($areas);
    }

    public function websitesAction()
    {
        $id = $this->getRequest()->getParam('id');
        $areas = array();
        //$collection = Mage::getModel('chinaarea/area')->getCollection()->addFieldToFilter('city_id',array('eq'=>$id))
        //    ->getSelect()->joinLeft(array('table_alias'=>'core_website'),'main_table.area_id=table_alias.area_id',array());

        /**
         * Get the resource model
         */
        $resource = Mage::getSingleton('core/resource');
        /**
         * Retrieve the read connection
         */
        $db = $resource->getConnection('core_read');
        $sql = "select `area`.*,website.website_id from directory_country_area `area` right join core_website website on `area`.area_id=website.area_id where `area`.city_id='{$id}'";
        $query = $db->query($sql);
        $collection = $query->fetchAll();
        $areas[] = array('value'=>'','label'=>'-请选择-');
        foreach($collection as $area)
        {
            $areas[] = array('value'=>$area['website_id'], 'label'=>$area['default_name']);
        }
        echo json_encode($areas);
    }

    public function linkAction()
    {
        $sku = $this->getRequest()->getParam('sku');
        if(!$sku) return;
        $model = Mage::getModel('catalog/product');
        $product = $model->load($model->getIdBySku($sku));
        if(!$product->getId()) return;
        echo Mage::helper('checkout/cart')->getAddUrl($product);
    }

    public function websiteAction()
    {
        $username = $this->getRequest()->getParam('username');
        $customer = null;
        if(preg_match("/^13[0-9]{1}[0-9]{8}$|15[0-9]{1}[0-9]{8}$|18[0-9]{1}[0-9]{8}$|17[0-9]{1}[0-9]{8}$/", $username))
        {
            $username = $username."@hahahui.cn";
            $collection = Mage::getModel('customer/customer')->getCollection()->addAttributeToFilter('email',array('eq'=>$username));
            $customer = $collection->getFirstItem();
        }
        else
        {
            //使用用户名登录首先根据firstname获取email然后登录
            $collection = Mage::getModel('customer/customer')->getCollection()->addAttributeToFilter('firstname',array('eq'=>$username));
            $customer = $collection->getFirstItem();
        }
        if(!$customer || !$customer->getId()) return;
        $websiteId = $customer->getWebsiteId();
        $website = Mage::getModel('core/website')->load($websiteId);
        if(!$website->getId()) return;
        $cookie = Mage::getSingleton('core/cookie');
        $cookie->set('run_type', 'website');
        $cookie->set('run_code', $website->getCode());
        echo "OK";
    }


    public function setAreaAction(){
        $websiteId = $this->getRequest()->getParam('websiteId');
        $website = Mage::getModel('core/website')->load($websiteId);
        if(!$website->getId()) return;
        $cookie = Mage::getSingleton('core/cookie');
        $cookie->set('run_type', 'website');
        $cookie->set('run_code', $website->getCode());
        echo "OK";
    }

    public function setUpdateCookieAction(){
       Mage::getSingleton('core/cookie')->set('not_update','yes',86400);
       echo "OK";
    }
}